package cn.ssm.petadopt.service.impl;

import cn.ssm.petadopt.codeMsg.CodeMsg;
import cn.ssm.petadopt.dto.DouPassword;
import cn.ssm.petadopt.dto.PwdSettingDTO;
import cn.ssm.petadopt.dto.ResisterDTO;
import cn.ssm.petadopt.dto.UserLoginDTO;
import cn.ssm.petadopt.entity.User;
import cn.ssm.petadopt.mapper.UserMapper;
import cn.ssm.petadopt.service.UserService;
import cn.ssm.petadopt.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


/**
 * @Autheor: Sun俊仪
 * @Date: 2022 2022/12/17 11:21
 * @Name: PetSystem
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    // 用户名查重
    @Override
    public ResponseResult judgeUserName(String userName) {
        ResponseResult responseResult = ResponseResult.builder().build();
        // 发送到持久层进行判断
        User user = userMapper.judgeUserName(userName);
        // 如果为空提示
        if (userName.equals("")){

        }
        if (user==null){
            // 设置反馈信息
            responseResult.setCode(CodeMsg.JUDGEUSERNAME_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.JUDGEUSERNAME_SUCCESS.getMsg());
            return  responseResult;
        }

        responseResult.setCode(CodeMsg.JUDGEUSERNAME_FAILED.getCode());
        responseResult.setMsg(CodeMsg.JUDGEUSERNAME_FAILED.getMsg());
        return  responseResult;

    }

    // 密码验重
    @Override
    public ResponseResult judgePwd(DouPassword douPassword) {
        ResponseResult responseResult = ResponseResult.builder().build();
        String firstPassword = douPassword.getPassword();
        String secondPassword = douPassword.getSecondPassword();

        if (firstPassword.equals(secondPassword)){
            responseResult.setCode(CodeMsg.JUDGEPWD_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.JUDGEPWD_SUCCESS.getMsg());
            return responseResult;
        }

        responseResult.setCode(CodeMsg.JUDGEPWD_FAILED.getCode());
        responseResult.setMsg(CodeMsg.JUDGEPWD_FAILED.getMsg());
        return responseResult;
    }

    // 注册
    @Override
    public ResponseResult registerUser(ResisterDTO resisterDTO, HttpSession session) {
        ResponseResult build = ResponseResult.builder().build();
        String code1 = resisterDTO.getCaptcha();
        System.out.println("code1 = " + code1);

        String userName = resisterDTO.getUserName();
        String password = resisterDTO.getPassword();
        String sex = resisterDTO.getSex();
        String address = resisterDTO.getAddress();
        Long age = resisterDTO.getAge();

        User user = User.builder().userName(userName).password(password).address(address).age(age).sex(sex).build();

        String code = (String) session.getAttribute("code");
        if (code.equals(code1)){
            Integer line = userMapper.registerUser(user);
            // 首先判断验证码  验证码相同才发送数据
            if (line!=null){        // 数据添加成功
                build.setMsg(CodeMsg.REGISTER_SUCCESS.getMsg());
                build.setCode(CodeMsg.REGISTER_SUCCESS.getCode());
                return build;
            }else {         // 数据添加失败
                build.setMsg(CodeMsg.REGISTER_FAILED.getMsg());
                build.setCode(CodeMsg.REGISTER_FAILED.getCode());
                return build;
            }
        }
        build.setMsg(CodeMsg.CODE_FAILED.getMsg());
        build.setCode(CodeMsg.CODE_FAILED.getCode());
        return build;





    }

    // 用户登录
    @Override
    public ResponseResult loginU(UserLoginDTO userLoginDTO, HttpSession session) {
        String userName = userLoginDTO.getUserName();
        String password = userLoginDTO.getPassword();
        String captcha = userLoginDTO.getCaptcha();
        ResponseResult responseResult = ResponseResult.builder().build();
        // 判断验证码
        String code = (String) session.getAttribute("code");
        if (captcha.equals(code)){
            // 通过userName从数据库中获取该user对象的信息
            User user = userMapper.judgeUserName(userName);
            if (user!=null){
                if (user.getPassword().equals(password)){
                    // 登录成功  将该用户信息存入到session域对象中
                    session.setAttribute("user",user);
                    // 返回成功登录信息      前端根据登录成功的code进行跳转
                    responseResult.setCode(CodeMsg.LOGIN_SUCCESS.getCode());
                    responseResult.setMsg(CodeMsg.LOGIN_SUCCESS.getMsg());

                }else {
                    // 密码错误
                    responseResult.setCode(CodeMsg.PASSWORD_FAILED.getCode());
                    responseResult.setMsg(CodeMsg.PASSWORD_FAILED.getMsg());
                }
            }else{
                // 用户名不存在
                responseResult.setCode(CodeMsg.USERNAME_FAILED.getCode());
                responseResult.setMsg(CodeMsg.USERNAME_FAILED.getMsg());
            }
        }else{
            //  返回验证码错误的msg
            responseResult.setCode(CodeMsg.CODE_FAILED.getCode());
            responseResult.setMsg(CodeMsg.CODE_FAILED.getMsg());
        }
        return responseResult;
    }

    // 修改个人资料
    @Override
    public ResponseResult updateU(User user) {
        ResponseResult responseResult = ResponseResult.builder().build();
        Integer line = userMapper.updateU(user);
        if (line!=null){
            responseResult.setCode(CodeMsg.UPDATEA_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATEA_SUCCESS.getMsg());
            return responseResult;
        }

        responseResult.setCode(CodeMsg.UPDATEA_FAILED.getCode());
        responseResult.setMsg(CodeMsg.UPDATEA_FAILED.getMsg());
        return responseResult;

    }

    // 数据回显修改个人资料
    @Override
    public ResponseResult selectShowSetting(String userName) {
        ResponseResult responseResult = ResponseResult.builder().build();
        User user = userMapper.selectShowSetting(userName);
        responseResult.setData(user);
        responseResult.setCode(200);
        return responseResult;
    }

    @Override
    public ResponseResult updatePwdSet(PwdSettingDTO pwdSettingDTO,HttpSession session) {
        String password = pwdSettingDTO.getPassword();
        String newPassword = pwdSettingDTO.getNewPassword();
        String againPassword = pwdSettingDTO.getAgainPassword();

        ResponseResult responseResult = ResponseResult.builder().build();

        User user = (User) session.getAttribute("user");
        String userName = user.getUserName();
        // 获取数据库中当前userName的信息
        User user1 = userMapper.selectShowSetting(userName);
        // 判断数据库中pwd和表单的form是否一致
        if (password.equals(user1.getPassword())){
            // 旧密码输入正确  ————》判断两次密码是否相同  相同更新数据库  同时更新session作用域中的密码
            if (newPassword.equals(againPassword)){
                // 更新session作用域中的数据
                user1.setPassword(newPassword);
                session.setAttribute("user",user1);
                // 修改数据库中的数据
                Integer line = userMapper.updatePwd(user1);
                if (line!=null){
                    responseResult.setCode(200);
                    responseResult.setMsg("密码修改成功");
                    return responseResult;
                }
            }else{
                responseResult.setCode(CodeMsg.JUDGEPWD_FAILED.getCode());
                responseResult.setMsg(CodeMsg.JUDGEPWD_FAILED.getMsg());
            }
        }else{
            responseResult.setCode(CodeMsg.OLDPWDSETTING_FAILED.getCode());
            responseResult.setMsg(CodeMsg.OLDPWDSETTING_FAILED.getMsg());
        }
        return responseResult;
    }
}
